package br.usp.ime.amazon;

import java.math.BigInteger;
import java.util.List;
import com.ECS.client.jax.*;

public class AmazonWS {
	private String AccessKeyId;
	private String SecretId;
	private String AssociateTag;
	
	AWSECommerceService service;
	AWSECommerceServicePortType port;
	
	public AmazonWS(String accessKeyId, String secretId, String associateTag) {
		super();
		AccessKeyId = accessKeyId;
		SecretId = secretId;
		AssociateTag = associateTag;
	}
	
	/*recebe uma lista de até 10 isbns e retorna detalhes dessas obras. O limite de 10 é dado pela amazon*/
	public ItemLookupResponse doIsbnLookup(List<String> isbnList){
		ItemLookup body = new ItemLookup();  
		body.setAWSAccessKeyId(AccessKeyId); 
		body.setAssociateTag(AssociateTag);

		ItemLookupRequest request = new ItemLookupRequest();
		request.setIdType("ASIN");
		request.getResponseGroup().add("Large");
		request.setCondition("New");
		request.setMerchantId("Amazon");
		for (String s: isbnList)
			request.getItemId().add(s);


		body.setShared(request);
		
		return port.itemLookup(body);
	};

	public void initAwsService(){
		service = new AWSECommerceService();
		service.setHandlerResolver(new AWSHandlerResolver(SecretId));
		port = service.getAWSECommerceServicePort();
	};
	
	/*realiza um powerSearch (busca baseada em uma string)*/
	public ItemSearchResponse doSearch(String searchIndex,
			String searchStr, int page) {
		
		ItemSearchRequest itemRequest = new ItemSearchRequest();

		itemRequest.setSearchIndex(searchIndex);
		itemRequest.setItemPage(BigInteger.valueOf(page));
		
		itemRequest.setPower(searchStr);

		ItemSearch itemSearch = new ItemSearch();
		itemSearch.setAWSAccessKeyId(AccessKeyId);
		itemSearch.setAssociateTag(AssociateTag);
		
		itemSearch.getRequest().add(itemRequest);

		return port.itemSearch(itemSearch);
	}

	
}
